package com.terrier.ui.effects
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	public class WxScaleEffect extends WxAbstractEffect
	{
		
		private var _scaleOld:Point;
		private var _targetPosition:Point;
		private var _scaleFrom:Point;
		private var _scaleTo:Point;
		private var _centerScale:Boolean = true;
		private var _scaleType:String;
		
		public function WxScaleEffect(scaleFrom:Point, scaleTo:Point, runTime:Number,
									  centerScale:Boolean = true, 
									  scaleType:String = WxEffectType.LINEAR,
									  target:DisplayObject = null,
									  startEventParams:Object = null,
									  updateEventParams:Object = null,
									  stopEventParams:Object = null)
		{
			super(target, runTime, startEventParams, updateEventParams, stopEventParams);
			
			this._scaleFrom = scaleFrom;
			this._scaleTo = scaleTo;
			this._centerScale = centerScale;
			this._scaleType = scaleType;
		}
		
		//--------------------------------------------------------
		//
		// Methods
		//
		//--------------------------------------------------------
		
		/**
		 * @private
		 */
		override protected function initialize():void
		{
			if(this.target != null)
			{
				this._scaleOld = new Point(this.target.scaleX, this.target.scaleY);
				var targetCenter:Point;
				if(this._centerScale)
				{
					targetCenter = new Point(this.target.x + (this.target.width / 2), this.target.y + (this.target.height / 2));
					this._targetPosition = new Point(
						targetCenter.x - ((this.target.width * this._scaleTo.x) / 2), 
						targetCenter.y - ((this.target.height * this._scaleTo.y) / 2)
					);				
				}
				else
				{
					this._targetPosition = new Point(this.target.x, this.target.y);
				}
				this.target.scaleX = this._scaleFrom.x;
				this.target.scaleY = this._scaleFrom.y;
				if(this._centerScale)
				{					
					this.target.x = targetCenter.x - (this.target.width * this._scaleFrom.x / 2);
					this.target.y = targetCenter.y - (this.target.height * this.scaleFrom.y / 2);
				}
			}
		}
		
		override public function start():void
		{
			super.start();
			if(target != null)
			{
				TweenLite.to(target, runTime,
					{x:this._targetPosition.x, y:this._targetPosition.y, 
					scaleX:this._scaleTo.x, scaleY:this._scaleTo.y,
					onComplete:stop});
			}
		}
		
		/**
		 * @private
		 */
		override public function restore():void
		{
			if(this.target != null)
			{
				this.target.scaleX = this._scaleOld.x;
				this.target.scaleY = this._scaleOld.y;
				this.target.x = this._targetPosition.x;
				this.target.y = this._targetPosition.y;
			}
		}
		
		public override function clone():IWxAbstractEffect
		{
			var newEffect:WxScaleEffect = new WxScaleEffect(
				this.scaleFrom, 
				this.scaleTo, 
				this.runTime, 
				this.centerScale, 
				this.scaleType,
				this.target, 
				this.startEventParams, 
				this.updateEventParams, 
				this.stopEventParams);
			return newEffect;
		}
		
		//--------------------------------------------------------
		//
		// Properties
		//
		//--------------------------------------------------------
		
		/**
		 * Starting scale
		 */
		public function get scaleFrom():Point
		{
			return _scaleFrom;
		}
		
		/**
		 * End scale
		 */
		public function get scaleTo():Point
		{
			return _scaleTo;
		}
		
		public function get scaleType():String
		{
			return this._scaleType;
		}
		
		public function get centerScale():Boolean
		{
			return this._centerScale;
		}
	}
}